package com.ruoyi.mx.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mx.domain.MxActivity;
import com.ruoyi.mx.domain.MxComment;
import com.ruoyi.mx.domain.MxGoodsSpu;
import com.ruoyi.mx.dto.ActivityDetail;
import com.ruoyi.mx.dto.CommentDetail;
import com.ruoyi.mx.service.IMxActivityService;
import com.ruoyi.mx.service.IMxCommentService;
import com.ruoyi.mx.service.IMxGoodsSpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/mx/nocheck")
@Api(tags = "前端免检控制器")
public class NoCheckController {
    @Autowired
    private IMxActivityService iMxActivityService;
    @Autowired
    private IMxCommentService iMxCommentService;
    @Autowired
    private IMxGoodsSpuService iMxGoodsSpuService;

    @GetMapping("getSwiperActivity")
    @Cacheable(value = "getSwiperActivity",key ="'swiper'",cacheManager="redisExpire")
    public R getSwipeActivity(){
        ArrayList<MxActivity> list=iMxActivityService.getSwiperActivity();
        return R.success("swiperList", list);
    }
    @GetMapping("getActivityList/{pageNum}/{pageSize}")
    @Cacheable(value = "getActivityList",key ="'pageNum_'+#pageNum+'pageSize'+#pageSize",cacheManager="redisExpire")
    public R getActivityList(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        ArrayList<MxActivity> list=iMxActivityService.getActivityList(pageNum,pageSize);
        return R.success("activityList", list);
    }
    @GetMapping("getHotActivityList/{pageNum}/{pageSize}")
    @Cacheable(value = "getHotActivityList",key ="'pageNum_'+#pageNum+'pageSize'+#pageSize",cacheManager="redisExpire")
    public R getHotActivityList(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        ArrayList<MxActivity> list=iMxActivityService.getHotActivityList(pageNum,pageSize);
        return R.success("activityList", list);
    }
    @GetMapping("getDetailById/{id}")
    //收藏数 不同时更新问题
    //@Cacheable(value = "getDetailById",key ="'id_'+#id",cacheManager="redisExpire")
    public R getDetailById(@PathVariable String id) {
        MxActivity activity = iMxActivityService.getById(id);
        if (StringUtils.isNotNull(activity)) {
            activity.setViews(activity.getViews() + 1);
            iMxActivityService.updateById(activity);
            ActivityDetail activityDetail = iMxActivityService.getDetailById(id);
            return R.success("获取活动详情成功", activityDetail);
        }else {
            return R.error("获取活动详情失败");
        }
    }

    @GetMapping("getPCommentsByAid/{aid}")
    @ApiOperation(value = "根据活动id查询一级评论")
    //@Cacheable(value = "getPCommentsByAid",key ="'aid_'+#aid",cacheManager="redisExpire")
    public R getPCommentsByCid(@PathVariable String aid){
        ArrayList<CommentDetail> list=iMxCommentService.getPCommentsByAid(aid);
        return R.success("查询一级评论成功",list);

    }

    @GetMapping("getCCommentsByCid/{cid}")
    @ApiOperation(value = "根据评论id查询子级评论")
    public R getCCommentsByCid(@PathVariable String cid){
        CommentDetail parentComment = iMxCommentService.getPCommentByCid(cid);
        List<CommentDetail> childComments=iMxCommentService.getChildComments(cid);
        HashMap<String,Object> map=new HashMap<>();
        map.put("parentComment",parentComment);
        map.put("childComments",childComments);
        return R.success("查询子级评论成功",map);
    }

    @GetMapping("getGoodsListByPidAndId/{Pid}/{id}/{pageNum}/{pageSize}")
    @ApiOperation(value = "根据父id和id查询商品列表")
    @Cacheable(value = "getGoodsListByPidAndId",key ="'Pid'+#Pid+'id'+#id",cacheManager="redisExpire")
    public R getGoodsListByPidAndId(@PathVariable String Pid,@PathVariable String id,@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        ArrayList<MxGoodsSpu> list=iMxGoodsSpuService.getGoodsListByPidAndId(Pid,id,pageNum,pageSize);
        return R.success("GoodsList", list);
    }

    @GetMapping("getGoodsDetailById/{id}")
    @ApiOperation(value = "根据id查询商品详情")
    @Cacheable(value = "getGoodsDetailById",key ="'id'+#id",cacheManager="redisExpire")
    public R getGoodsDetailById(@PathVariable("id" ) String id) {
        MxGoodsSpu goodsDetail = iMxGoodsSpuService.getById(id);
        if(StringUtils.isNotNull(goodsDetail)){
            return R.success("获取商品详情成功",goodsDetail);
        }else{
            return R.error("获取商品详情失败");
        }
    }

    @GetMapping("getGoodsListByName/{name}")
    @ApiOperation(value = "根据商品名称模糊查询商品列表")
    @Cacheable(value = "getGoodsListByName",key ="'name'+#name",cacheManager="redisExpire")
    public R getGoodsListByName(@PathVariable("name" ) String name) {
        ArrayList<MxGoodsSpu> goodsList=iMxGoodsSpuService.getGoodsListByName(name);
        if(StringUtils.isNotNull(goodsList)){
            return R.success("获取商品列表成功",goodsList);
        }else{
            return R.error("获取商品列表失败");
        }
    }

    @GetMapping("getGoodsListByNameOrderAsc/{name}")
    @ApiOperation(value = "根据商品名称价格升序查询商品列表")
    @Cacheable(value = "getGoodsListByNameOrderAsc",key ="'name'+#name",cacheManager="redisExpire")
    public R getGoodsListByNameOrderAsc(@PathVariable("name" ) String name) {
        ArrayList<MxGoodsSpu> goodsList=iMxGoodsSpuService.getGoodsListByNameOrderAsc(name);
        if(StringUtils.isNotNull(goodsList)){
            return R.success("获取商品列表成功",goodsList);
        }else{
            return R.error("获取商品列表失败");
        }
    }

    @GetMapping("getGoodsListByNameOrderDesc/{name}")
    @ApiOperation(value = "根据商品名称价格降序查询商品列表")
    @Cacheable(value = "getGoodsListByNameOrderDesc",key ="'name'+#name",cacheManager="redisExpire")
    public R getGoodsListByNameOrderDesc(@PathVariable("name" ) String name) {
        ArrayList<MxGoodsSpu> goodsList=iMxGoodsSpuService.getGoodsListByNameOrderDesc(name);
        if(StringUtils.isNotNull(goodsList)){
            return R.success("获取商品列表成功",goodsList);
        }else{
            return R.error("获取商品列表失败");
        }
    }

    @GetMapping("getGoodsListByNameOrderSaleAsc/{name}")
    @ApiOperation(value = "根据商品名称销量升序查询商品列表")
    @Cacheable(value = "getGoodsListByNameOrderSaleAsc",key ="'name'+#name",cacheManager="redisExpire")
    public R getGoodsListByNameOrderSaleAsc(@PathVariable("name" ) String name) {
        ArrayList<MxGoodsSpu> goodsList=iMxGoodsSpuService.getGoodsListByNameOrderSaleAsc(name);
        if(StringUtils.isNotNull(goodsList)){
            return R.success("获取商品列表成功",goodsList);
        }else{
            return R.error("获取商品列表失败");
        }
    }

    @GetMapping("getGoodsListByNameOrderSaleDesc/{name}")
    @ApiOperation(value = "根据商品名称销量降序查询商品列表")
    @Cacheable(value = "getGoodsListByNameOrderSaleDesc",key ="'name'+#name",cacheManager="redisExpire")
    public R getGoodsListByNameOrderSaleDesc(@PathVariable("name" ) String name) {
        ArrayList<MxGoodsSpu> goodsList=iMxGoodsSpuService.getGoodsListByNameOrderSaleDesc(name);
        if(StringUtils.isNotNull(goodsList)){
            return R.success("获取商品列表成功",goodsList);
        }else{
            return R.error("获取商品列表失败");
        }
    }

    @GetMapping("getGoodsListByNameOrderTimeDesc/{name}")
    @ApiOperation(value = "根据商品名称新上架降序查询商品列表")
    @Cacheable(value = "getGoodsListByNameOrderTimeDesc",key ="'name'+#name",cacheManager="redisExpire")
    public R getGoodsListByNameOrderTimeDesc(@PathVariable("name" ) String name) {
        ArrayList<MxGoodsSpu> goodsList=iMxGoodsSpuService.getGoodsListByNameOrderTimeDesc(name);
        if(StringUtils.isNotNull(goodsList)){
            return R.success("获取商品列表成功",goodsList);
        }else{
            return R.error("获取商品列表失败");
        }
    }




}
